﻿using System.Collections;

namespace LDM.Framework.ExceptionHandling.Extensions
{
    /// <summary>
    /// 异常抛出拓展
    /// </summary>
    public static class BusinessExceptionExtensions
    {
        /// <summary>
        /// 验证实体是否为null
        /// </summary>
        /// <param name="entity">数据源</param>
        /// <param name="throwMsg">错误信息</param>
        public static T ThrowIfNull<T>(this T entity, string throwMsg = "非法请求") where T : class
        {
            //Discarded unreachable code: IL_0002
            if (entity is string value && string.IsNullOrWhiteSpace(value))
            {
                throw new BusinessException(throwMsg);
            }
            if (entity == null)
            {
                throw new BusinessException(throwMsg);
            }
            return entity;
        }

        /// <summary>
        /// 验证集合是否为空
        /// </summary>
        /// <param name="entity">数据源</param>
        /// <param name="throwMsg">错误信息</param>
        public static T ThrowIfNullOrEmpty<T>(this T entity, string throwMsg = "非法请求") where T : class
        {
            //Discarded unreachable code: IL_0002
            entity.ThrowIfNull(throwMsg);
            if (entity is ICollection self && self.IsNullEmpty())
            {
                throw new BusinessException(throwMsg);
            }
            return entity;
        }

        /// <summary>
        /// 验证实体的属性是否为null
        /// </summary>
        /// <typeparam name="T">数据源类型</typeparam>
        /// <typeparam name="P">属性类型</typeparam>
        /// <param name="entity">数据源</param>
        /// <param name="property">属性</param>
        /// <param name="propertyName">属性名</param>
        public static T ThrowIfPropertyIsNull<T, P>(this T entity, Func<T, P> property, string propertyName) where T : class
        {
            //Discarded unreachable code: IL_0002
            entity.ThrowIfNull();
            if (property(entity) == null)
            {
                throw new BusinessException(propertyName);
            }
            return entity;
        }

        /// <summary>
        /// 验证实体的字符串属性是否为null或空字符串
        /// </summary>      
        /// <typeparam name="T">数据源类型</typeparam>
        /// <param name="entity">数据源</param>
        /// <param name="property">属性</param>
        /// <param name="propertyName">属性名</param>
        public static T ThrowIfPropertyIsNullOrEmpty<T>(this T entity, Func<T, string> property, string propertyName) where T : class
        {
            //Discarded unreachable code: IL_0002
            entity.ThrowIfNull();
            if (string.IsNullOrWhiteSpace(property(entity)))
            {
                throw new BusinessException(propertyName);
            }
            return entity;
        }

        /// <summary>
        /// 自定义实体验证约束规则并验证
        /// </summary>
        /// <typeparam name="T">数据源类型</typeparam>
        /// <param name="entity">数据源</param>
        /// <param name="check">验证约束规则</param>
        /// <param name="throwMsg">错误提示</param>
        public static T ThrowIfCheckError<T>(this T entity, Predicate<T> check, string throwMsg) where T : class
        {
            //Discarded unreachable code: IL_0002
            if (!check(entity))
            {
                throw new BusinessException(throwMsg);
            }
            return entity;
        }

        /// <summary>
        /// 当满足指定异常条件后,异常返回
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity">检查对象</param>
        /// <param name="check">检查条件</param>
        /// <param name="msg">异常消息</param>
        /// <param name="redirect">跳转信息</param>
        /// <returns></returns>
        public static T ThrowIf<T>(this T entity, Predicate<T> check, string msg) where T : class
        {
            //Discarded unreachable code: IL_0002
            if (check(entity))
            {
                throw new BusinessException(msg);
            }
            return entity;
        }

        /// <summary>
        /// 当满足指定异常条件后,异常返回
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity">检查对象</param>
        /// <param name="check">检查条件</param>
        /// <param name="msgFormart">异常消息模板</param>
        /// <param name="redirect">跳转信息</param>
        /// <param name="args">消息参数</param>
        /// <returns></returns>
        public static T ThrowIf<T>(this T entity, Predicate<T> check, string msgFormart, params object[] args) where T : class
        {
            //Discarded unreachable code: IL_0002
            if (check(entity))
            {
                throw new BusinessException(msgFormart);
            }
            return entity;
        }

        /// <summary>
        /// 是否是空集合对象
        /// </summary>
        /// <param name="self">数据源</param>
        /// <returns></returns>
        public static bool IsNullEmpty(this ICollection self)
        {
            //Discarded unreachable code: IL_0002, IL_0028
            if (self != null)
            {
                return self.Count == 0;
            }
            return true;
        }
    }
}
